import dashscope
from dashscope.aigc.generation import Generation
from dashscope.api_entities.dashscope_response import Role
from dashscope.aigc import Generation
from os import getenv


def stream_commit_analysis(prompt_text: str):
    """
    使用 DashScope 通义千问生成流式回复
    """
    messages = [
        {'role': Role.SYSTEM, 'content': '你是资深软件工程师，擅长解析 Git 提交。'},
        {'role': Role.USER, 'content': prompt_text},
    ]

    responses = Generation.call(
        api_key=getenv("QWEN_KEY"),
        model=Generation.Models.qwen_max,
        messages=messages,
        result_format='message',
        incremental_output=True,
        stream=True
    )

    for response in responses:
        content = response.get("output", {}).get("choices", [{}])[
            0].get("message", {}).get("content")
        if content:
            yield content
